<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 用户类  
 */
class Task extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->library('request');  //自定义
        $this->load->library('response'); //自定义
        $this->load->library('session'); //公共 
    }   


   /**
     * [taskList 不绑定手机号也可先查看]
     * @return [type] [description]
    */
    public function taskList(){
        if($this->input->method()=="get"){       
            $reqData = $this->request->getGetData(array("selected","type","min","max","id","pageSize"));
            log_message('info', 'task/taskList,data:'.json_encode($reqData)); 

            //1验证登录状态 
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            // var_dump($retStatus);exit; 
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;
            }
            $userId = $retStatus['userId'];
            // //2手机号  不需要
            // $base =  $this->userModel->baseInfo($userId);
            // if(!(isset($base->account) && $base->account)){
            //     $this->response->sendData(2018);return;                
            // }
            // //3身份认证  不需要
            // $extra = $this->userModel->extraInfo($userId);
            // if(!(isset($base->id_status)&&$base->id_status=1)){
            //     $this->response->sendData(2019);return;                
            // }

            //4查询任务列表
            $this->load->model("taskModel");
            $list = $this->taskModel->taskList($reqData);
            // var_dump($list);exit;
            $this->response->sendData($list['status'],$list['data']);return ;    
            
          
        }
        else{
            $this->response->sendData(1001);return ;
        }

    } 

    /**
     * [taskDetail 用户查询未被领取的任务
     * 前提：手机号]
     * @return [type] [description]
    */
    public function taskDetail(){
        if($this->input->method()=="get"){       
            $reqData = $this->request->getGetData(array("id"));
            log_message('info', 'task/taskDetail,data:'.json_encode($reqData)); 
            if(!$reqData['id']){
                $this->response->sendData(1000);return;                
            }

            //1验证登录状态 
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            // var_dump($retStatus);exit; 
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;
            }
            $userId = $retStatus['userId'];
            
            //2手机号
            $base =  $this->userModel->baseInfo($userId);
            if(!(isset($base->account) && $base->account)){
                $this->response->sendData(2018);return;                
            }
          
            //3查询详情
            $this->load->model("taskModel");
            // var_dump($reqData);exit;
            $taskDetail =  $this->taskModel->taskDetail($reqData['id']);
            if($taskDetail['out_data']->status!=0){
                //说明被人领取了，此接口仅查询未被领取的
                $this->response->sendData(3012);return;       
                
            }
            
            //4身份认证情况
            $extra = $this->userModel->extraInfo($userId);
            // var_dump($extra);exit;
            if(!(isset($extra->id_status)&&$extra->id_status==1)){
                $taskDetail['out_data']->identifyStatus = 0;          
            }
            else{
                $taskDetail['out_data']->identifyStatus = 1;                         
            }

            //5邮箱状态
            if(!(isset($extra->id_status)&&$extra->email)){
                $taskDetail['out_data']->emailStatus = 0;         
            }
            else{
                $taskDetail['out_data']->emailStatus = 1;
            }

            //6学生的额外奖励 
            if(isset($extra->school_status)&&$extra->school_status==1){
                //是学生  
                $taskDetail['out_data']->studentAward = $this->config->item('studentAward');
            }
            else{
                //不是或者没有认证通过
                $taskDetail['out_data']->studentAward = 0;
            }
            //7保障金
            if($this->config->item('percent')<0){
                $taskDetail['out_data']->deposit =  $this->config->item('percentPrice');
            }
            else{
                $taskDetail['out_data']->deposit =  floor($taskDetail['out_data']->reward * $this->config->item('percent'));
            }

            
            $this->response->sendData(0,'',$taskDetail['out_data']);return;
          
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);

    } 

    /**
     * [taskConfirmDetail 
     * 自己领取的任务的页面详情 ]
     * @return [type] [description]
    */
    public function taskConfirmDetail(){
        if($this->input->method()=="get"){       
            $reqData = $this->request->getGetData(array("id"));
            log_message('info', 'task/taskConfirmDetail,data:'.json_encode($reqData)); 
            if(!$reqData['id']){
                $this->response->sendData(1000);return;                
            }

            //1验证登录状态 
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            // var_dump($retStatus);exit; 
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;
            }
            $userId = $retStatus['userId'];
            // echo $userId;exit;
            //2手机号
            $base =  $this->userModel->baseInfo($userId);
            if(!(isset($base->account) && $base->account)){
                $this->response->sendData(2018);return;                
            }
            //3邮箱
            $extra =  $this->userModel->extraInfo($userId);
            if(!(isset($extra->id_status)&&$extra->email)){
                $this->response->sendData(2020);return;                
            }
            //4查询详情
            $this->load->model("taskModel");
            $taskObtainDetail =  $this->taskModel->taskObtainDetailById($reqData['id'],$userId);
            if(!$taskObtainDetail){
                $this->response->sendData(3006);return;                
            }
             
            // var_dump($reqData);exit;
            $taskDetail =  $this->taskModel->taskDetail($taskObtainDetail->task_id,1);
            if(isset($taskDetail['out_data']->id)){
                $taskDetail['out_data']->obtainStatus = $taskObtainDetail->status;//任务领取的状态
            }
            //5如果取消任务，参数1是否有提交，参数2具体扣款金额
            $cancelParma = $this->taskModel->cancelParma($taskObtainDetail->task_id);
            if(isset($taskDetail['out_data']->id)){
                $taskDetail['out_data']->submitCount = $cancelParma['submit'];                
            }
            //6计算扣除金额，扣押金
            $deduction = $this->taskModel->calcDeduction($userId,$taskObtainDetail->task_id,$cancelParma['submit'],$taskDetail['out_data']->closeDate,$taskDetail['out_data']->closeTime);
            $taskDetail['out_data']->deduction = $deduction['deduction'];
            
            $this->response->sendData(0,'',$taskDetail['out_data']);return;
          
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);

    }

    // public function test(){
    //     $this->load->model("taskModel");
    //     $ret = $this->taskModel->calcDeduction(64,1,'2018-11-13','',10);
    //     var_dump($ret);
    // }


    /**
     * [commitTask 用户提交自己的一个任务]
     * @return [type] [description]
    */
    public function commitTask(){
        if($this->input->method()=="post"){       
            $reqData = $this->request->getPostRawData();
            log_message('info', 'task/commitTask,data:'.json_encode($reqData)); 
            if(!$reqData['id']){
                $this->response->sendData(1000);return;
            }
            // var_dump($reqData);exit;
            //1验证登录状态
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;

            }
            // var_dump($retStatus);exit;
            $userId = $retStatus['userId'];
            
           
            //2任务领取状态
            $this->load->model("taskModel");
            $taskObtainInfo = $this->taskModel->taskObtainDetailById($reqData['id'],$userId);
            if(!isset($taskObtainInfo->id)){//不是自己领取的任务
                $this->response->sendData(1004);return; //无权限
            }
            if(!($taskObtainInfo->status==0||$taskObtainInfo->status==4||$taskObtainInfo->status==5)){
                //0正常，4延期开发，5返工修改
                $this->response->sendData(3002);return; //状态有误
            }

            //3是否是自己的任务
            $taskInfo = $this->taskModel->taskDetail($taskObtainInfo->task_id);
            if(!isset($taskInfo['out_data']->id)){
                $this->response->sendData(3000);return;

            }
            if($taskInfo['out_data']->status!=1){
                $this->response->sendData(3001);return; //状态有误
            }
            

            //4是否超时，超时不可提交
            $overtimeStatus = $this->taskModel->overtimeStatus($taskInfo['out_data']->closeDate,$taskInfo['out_data']->closeTime);
            if($overtimeStatus){
                $this->response->sendData(3013);return; //状态有误                
            }
            
            //
            //5提交（改数据库状态）
            $status = $this->taskModel->updateObtainTask($taskObtainInfo->id,1);
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);

    }


    /**
     * [cancelTask 用户申请取消一个任务]
     * @return [type] [description]
    */
    public function cancelTask(){
        if($this->input->method()=="post"){       
            $reqData = $this->request->getPostRawData();
            log_message('info', 'task/cancelTask,data:'.json_encode($reqData)); 
            if(!$reqData['id'] || !$reqData['instruction']){
                $this->response->sendData(1000);return;
            }
            // var_dump($reqData);exit;
            //1验证登录状态
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;

            }
            // var_dump($retStatus);exit;
            $userId = $retStatus['userId'];
            
            //2任务领取状态
            $this->load->model("taskModel");
            $taskObtainInfo = $this->taskModel->taskObtainDetailById($reqData['id'],$userId);
            if(!isset($taskObtainInfo->id)){//不是自己领取的任务
                $this->response->sendData(1004);return; //无权限
            }


            if(!($taskObtainInfo->status==0 || $taskObtainInfo->status==4||$taskObtainInfo->status==5)) {
                $this->response->sendData(3002);return; //状态有误
            }

            // 3是否是自己的任务
            $taskInfo = $this->taskModel->taskDetail($taskObtainInfo->task_id);
            // var_dump($taskInfo);exit;
            if(!isset($taskInfo['out_data']->id)){
                $this->response->sendData(3000);return;

            }
            if($taskInfo['out_data']->status!=1){
                $this->response->sendData(3001);return; //状态有误
            }
            //4同一个任务，只能申请取消2次
            $taskObtainLog = $this->taskModel->taskObtainLog($taskObtainInfo->task_id,$userId,6);
            if($taskObtainLog>=2){
                $this->response->sendData(3007);return; //无权限
                
            }
            
            //5取消（改数据库状态）
            $reqData['userId'] = $userId;
            $reqData['closeDate'] = $taskInfo['out_data']->closeDate;
            $reqData['closeTime'] = $taskInfo['out_data']->closeTime;
            $status = $this->taskModel->updateObtainTask($taskObtainInfo->id,6,$reqData);
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);

    }

    /**
     * [delayTask 用户申请延期一个任务，同一个任务，可申请2次，做多有一次成功]
     * @return [type] [description]
    */
    public function delayTask(){
        if($this->input->method()=="post"){       
            $reqData = $this->request->getPostRawData();
            log_message('info', 'task/delayTask,data:'.json_encode($reqData)); 
            if(empty($reqData['id']) || empty($reqData['instruction'])|| empty($reqData['closeDate'])|| empty($reqData['closeTime']) || !strtotime($reqData['closeDate']) ){
                $this->response->sendData(1000);return;
            }
            // var_dump($reqData);exit;
            //1验证登录状态
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;

            }
            // var_dump($retStatus);exit;
            $userId = $retStatus['userId'];
            
            //2任务领取状态
            $this->load->model("taskModel");
            $taskObtainInfo = $this->taskModel->taskObtainDetailById($reqData['id'],$userId);
            if(!isset($taskObtainInfo->id)){//不是自己领取的任务
                $this->response->sendData(1004);return; //无权限
            }
            if(!($taskObtainInfo->status==0 || $taskObtainInfo->status==4||$taskObtainInfo->status==5)) {
                $this->response->sendData(3002);return; //状态有误
            }

            //3是否是自己的任务
            $taskInfo = $this->taskModel->taskDetail($taskObtainInfo->task_id);
            if(!isset($taskInfo['out_data']->id)){
                $this->response->sendData(3000);return;

            }
            if($taskInfo['out_data']->status!=1){
                $this->response->sendData(3001);return; //状态有误
            }
            //4同一个任务，可申请2次，最多有一次成功
            $taskObtainLog = $this->taskModel->taskObtainLog($taskObtainInfo->task_id,$userId,3);
            if($taskObtainLog>=2){
                $this->response->sendData(3008);return; // 

            }
            //5，最多有一次成功
            // var_dump($taskObtainInfo);exit;
            $taskObtainLog = $this->taskModel->taskObtainLog($taskObtainInfo->task_id,$userId,4);
            // echo $taskObtainLog;exit;
            if($taskObtainLog>=1){
                $this->response->sendData(3009);return; // 

            }
            
            //6延期（改数据库状态）
            if($taskInfo['out_data']->closeDate>$reqData['closeDate']){                
                $this->response->sendData(3023);return; // 
            }
            $pos = strpos($reqData['closeTime'], ":");
            if($pos>=2){
                $reqData['closeTime'] = substr($reqData['closeTime'], $pos-2,8);
            }
            $reqData['delaycloseDate'] = $reqData['closeDate'] ;
            $reqData['delaycloseTime'] =  $reqData['closeTime'];
            $reqData['closeDate'] = $taskInfo['out_data']->closeDate;
            $reqData['closeTime'] = $taskInfo['out_data']->closeTime;
            $reqData['taskId'] = $taskObtainInfo->task_id;
            $reqData['userId'] = $userId;
            $status = $this->taskModel->updateObtainTask($taskObtainInfo->id,3,$reqData);
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);

    }

    


   /**
     * [myObtainTaskList]
     * @return [type] [description]
    */
    public function myObtainTaskList(){
        if($this->input->method()=="get"){       
            $reqData = $this->request->getGetData(array("id","status","pageSize"));
            log_message('info', 'task/myObtainTaskList,data:'.json_encode($reqData)); 

            //1验证登录状态 
            $this->load->model("userModel");
            $retStatus =  $this->userModel->loginStatus($reqData);
            // var_dump($retStatus);exit; 
            if($retStatus['status']){
                $this->response->sendData($retStatus['status']);return;
            }
            $userId = $retStatus['userId'];

            //2查询我的任务列表
            $this->load->model("taskModel");
            $list = $this->taskModel->myObtainTask($userId,$reqData);
            // var_dump($list);exit;
            $this->response->sendData(0,$list);return ;    
            
          
        }
        else{
            $this->response->sendData(1001);return ;
        }

    } 

    
}